package com.yxkj.syh.app.huarong.adps;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.utils.ComUtil;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.Goods;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/5/26 15:26
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class GoodsListAdp extends RecyclerView.Adapter<GoodsListAdp.ViewHolder> {
    List<Goods> data = new ArrayList<>();
    private OnClick mOnClick;
    private String preSelectedGoodsCode;
    private Goods preSelectedDiyGoods;
    private Context mContext;

    public GoodsListAdp(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_list, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = data.get(position);
        switch (goods.getLableStatus()) {
            case 0:
                holder.llGoods.setBackgroundResource(R.drawable.shape_lablegroup_lable_bg0);
                holder.tvName.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.textColor1));
                holder.ivSelected.setVisibility(View.GONE);
                break;
            case 1:
                holder.tvName.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.textColor3));
                holder.llGoods.setBackgroundResource(R.drawable.shape_lablegroup_lable_bg0);
                holder.ivSelected.setVisibility(View.GONE);
                break;
            case 2:
                holder.tvName.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.defaultBlue));
                holder.llGoods.setBackgroundResource(R.drawable.shape_lablegroup_lable_bg2);
                holder.ivSelected.setVisibility(View.VISIBLE);
                break;
        }
        if (TextUtils.isEmpty(preSelectedGoodsCode)) {
            if (!goods.isAdded()) {
                holder.itemView.setOnClickListener(v -> {
                    if (mOnClick != null) {
                        mOnClick.oneLableClick(goods);
                    }
                    for (Goods temp : data) {
                        if (temp.getCode().equals(goods.getCode())) {//当前
                            temp.setLableStatus(2);
                        } else {
                            if (temp.isAdded()) {
                                temp.setLableStatus(1);
                            } else {
                                temp.setLableStatus(0);
                            }
                        }
                    }
                    notifyDataSetChanged();
                });
            }
        } else {
            if (preSelectedDiyGoods != null &&
                    preSelectedDiyGoods.getNo() != null && preSelectedDiyGoods.getNo().equals("custom")) {
                goods.setName(preSelectedDiyGoods.getName());
            }
        }
        holder.tvName.setText(goods.getName());


        LinearLayout.LayoutParams ivP = (LinearLayout.LayoutParams) holder.ivEp.getLayoutParams();
        int width = (AppUtil.getScreenWidths(mContext) - ComUtil.Dp2Px(mContext, (40 + 10 + 8))) / 2;
        int height = (int) (width * 0.65f);
        ivP.width = width;
        ivP.height = height;
        holder.ivEp.setLayoutParams(ivP);
//
//        //设置图片圆角角度
//        RoundedCorners roundedCorners= new RoundedCorners(ComUtil.Dp2Px(mContext,4));
//        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
//        RequestOptions options=RequestOptions.bitmapTransform(roundedCorners).override(width, height);

        Glide.with(mContext).load(goods.getPicture()).apply(RequestOptions.errorOf(R.mipmap.ic_def_goods)).into(holder.ivEp);

    }


    public void setNewData(List<Goods> goods) {
        data.clear();
        data.addAll(goods);
        notifyDataSetChanged();
    }

    public List<Goods> getData() {
        return data;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setOnClick(OnClick onClick) {
        this.mOnClick = onClick;
    }

    public void setPreSelectedGoodsCode(String preSelectedGoodsCode) {
        this.preSelectedGoodsCode = preSelectedGoodsCode;
    }

    public void setPreSelectedGoodsInDiy(Goods goods) {
        this.preSelectedDiyGoods = goods;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        ImageView ivSelected;
        ImageView ivEp;
        LinearLayout llGoods;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            llGoods = itemView.findViewById(R.id.llGoods);
            ivSelected = itemView.findViewById(R.id.ivSelected);
            ivEp = itemView.findViewById(R.id.ivEp);
        }
    }

    public interface OnClick {
        void oneLableClick(Goods goods);
    }


}
